package com.ajx.cube.module.splash;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;


import com.ajx.core.app.Cube;

import com.ajx.cube.common.Constants;
import com.ajx.cube.module.login.LoginActivity;
import com.ajx.cube.module.splash.lancher.LauncherActivity;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SPUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * Created by lai on 2017/5/26.
 */
public class SplashActivity extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*set it to be full screen*/
        hideBottomUIMenu();
        checkNetWork();
    }

    private void checkNetWork() {
        boolean connected = NetworkUtils.isConnected();
        if (connected){
            checkLogin();
        }else {
            new AlertDialog.Builder(this)
                    .setTitle("网络不可用")
                    .setMessage("网络不可用，请连接网络")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.cancel();
                            finish();
                        }
                    })
                    .create().show();
        }

    }



    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }
    private void checkLogin() {
        SPUtils sp = Cube.getSp();
        boolean firstLauncher = sp.getBoolean(Constants.SpFlag.FIRST_LAUNCH, true);
        if (firstLauncher){
            Intent intent=new Intent(this,LauncherActivity.class);
            startActivity(intent);
            finish();
        }else {
            boolean hasLogin = sp.getBoolean(Constants.SpFlag.HAS_LOGIN, false);
            if (hasLogin){//进入主页面

            }else {
                Intent intent=new Intent(this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }



    private void go(final Class clz) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent=new Intent(SplashActivity.this,clz);
                intent.putExtra("from","splash");
                startActivity(intent);
                finish();
            }
        }, 1500);
    }

    public int daysBetween(Date smdate, Date bgdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bgdate = sdf.parse(sdf.format(bgdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bgdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }


}
